package com.example.demo.Utils;


import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.exception.CosServiceException;
import com.qcloud.cos.model.ListObjectsRequest;
import com.qcloud.cos.model.ObjectListing;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.region.Region;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

public class CosUtils {
    private static final String BUCKETNAME = "qcode-1300972980";
    public static final String AVATAR_NAME= "avatar";
    private static COSClient createBucket(){
        String SECRETID = "AKIDo5D6pJOtWtMejd5nIcvKQT3VRDvPrkdI";
        String SECRETKEY = "kGZegTEm68z82zKVUIsIjmcidMvHNIxz";
        COSCredentials cred = new BasicCOSCredentials(SECRETID, SECRETKEY);
        Region region = new Region("ap-guangzhou");
        ClientConfig clientConfig = new ClientConfig(region);
        return new COSClient(cred, clientConfig);
    }


// 用户点击扫码 生成二维码图上传到cos上，   服务端 生成 uuid 作为键值 key  存储到redis 上值为0  并返回给客户端
//    客户端 使用SetTimeOut 访问  uuid  对应的 状态value
//     小程序扫码后  调用连接 将服务器上    将小程序端的token  发送到 redis 上  redis 上 修改值为 token
//   如果返回状态码不为0    存储返回值token   则停止访问   前端提示扫码成功 调用token



    public void setDir(String dirName)
    {
        COSClient cosClient =createBucket();
        try {
            String dirPath =  dirName+"/";
            InputStream inputStream = new ByteArrayInputStream(new byte[0]);
            ObjectMetadata objectdirMetadata = new ObjectMetadata();
            objectdirMetadata.setContentLength(0);
            PutObjectRequest putObjectRequest =
                    new PutObjectRequest(BUCKETNAME, dirPath, inputStream, objectdirMetadata);
            cosClient.putObject(putObjectRequest);
        } catch (CosClientException serverException) {
            serverException.printStackTrace();
        } finally {
            cosClient.shutdown();
        }
    }

    public static String setFile(String dirName  ,String userName, MultipartFile[] files) {
        COSClient cosClient = createBucket();
        System.out.println("进入到文件上传方法");
        System.out.println(files.length);
        try {
            for (MultipartFile file : files) {
                String originalFilename = file.getOriginalFilename();
                System.out.println("接收到文件：" + originalFilename);
                String dirPath = dirName + "/" + userName + "/";
                InputStream localFile = file.getInputStream();
                ObjectMetadata objectMetadata = new ObjectMetadata();
                objectMetadata.setContentLength(file.getSize());
                objectMetadata.setContentType(file.getContentType());
                String key = dirPath + originalFilename;
                PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKETNAME, key, localFile, objectMetadata);
                cosClient.putObject(putObjectRequest);
                localFile.close();
                return key;
            }
        } catch (CosServiceException | IOException serverException) {
            serverException.printStackTrace();
        } finally {
            cosClient.shutdown();
        }
        return null;
    }
}
